//
// Created by ken on 2025/2/20.
//
#include "Driver_Usart.h"

/**
 *
 * @param ReceiveData
 * @param ValueName
 * @param Length_ValueName
 * @param Before_Value
 * @param Length_Value
 * @return
 */
uint16_t Change_ReceiveData(char *ReceiveData, char *ValueName,
                            uint16_t Length_ValueName, uint16_t Before_Value,
                            uint16_t Length_Value) {
  // 改变后的变量值
  uint16_t After_Value;

  // 暂存字符
  char Temp_RxPacket[Length_ValueName];
  // 暂存改变值变量
  char Temp_Value[Length_Value];

  memset(Temp_Value, '\0', sizeof(Temp_Value));

  strncpy(Temp_RxPacket, ReceiveData,
          Length_ValueName); // 将接收到的字符串前n位取出,存在Temp_RxPacket中

  if (strcmp(Temp_RxPacket, ValueName) == 0) { // 判断是否等于需要修改的变量名
    strncpy(Temp_Value, ReceiveData + Length_ValueName,
            Length_Value); // 将变量改变后的值，写入到暂存字符数组中
    After_Value = (uint16_t)atoi(Temp_Value);
  } else {
    After_Value = Before_Value;
  }
  return After_Value;
}

/**
 * 判断接受的值是否与想要的相同
 * 相同返回：1 不同返回：0
 * @param ReceiveData
 * @param ValueName
 * @param Length_ValueName
 * @return
 */
uint8_t Judge_Receive(char *ReceiveData, char *ValueName,
                      uint16_t Length_ValueName) {
  // 暂存字符
  char Temp_RxPacket[Length_ValueName];
  // 是否相同标志位
  uint8_t Flag = 0;

  strncpy(Temp_RxPacket, ReceiveData,
          Length_ValueName); // 将接收到的字符串前n位取出,存在Temp_RxPacket中

  if (strcmp(Temp_RxPacket, ValueName) == 0) { // 判断是否等于需要修改的变量名
    Flag = 1;
  }
  return Flag;
}

void Usart2_Printf(char *format, ...) {
  char String[100];
  va_list arg;
  va_start(arg, format);
  vsprintf(String, format, arg);
  va_end(arg);
  HAL_UART_Transmit_DMA(&huart2, (uint8_t *)&String, strlen(String));
}